#include "ia09.h"

void selectColors(int * cls, int numColors, int ind, int numBalls)
{
  if (ind == numBalls)
    {
      printBalls(cls, numBalls);
      return;
    }
  int iter;
  for (iter = RED; iter < RED + numColors; iter ++)
    {
      if ((ind == 0) || (cls[ind - 1] != RED) || (iter != RED))
	{
	  cls[ind] = iter;
	  selectColors(cls, numColors, ind + 1, numBalls);
	}
    }
}

void selectBalls(int numColors, int numBalls)
{
  int * cls = malloc(sizeof(int) * numBalls);
  selectColors(cls, numColors, 0, numBalls);
  free (cls);
}

void partition(int * usedInts, int value, int ind, int minValue)
{
  if (value == 0)
    {
      printPartition(usedInts, ind);
      return;
    }
  int iter;
  for (iter = minValue; iter <= value; iter ++)
    {
      usedInts[ind] = iter;
      partition(usedInts, value - iter, ind + 1, minValue);
    }
}

void partitionInteger(int value, int minValue)
{
  int * usedInts = malloc(sizeof(int) * value);
  partition(usedInts, value, 0, minValue);
  free (usedInts);  
}

void subset(int * includeElement, int ind, int size, int maxSize)
{
  if (ind == size)
    {
      printSubset(includeElement, size, maxSize);
      return;
    }
  includeElement[ind] = 0;
  subset(includeElement, ind + 1, size, maxSize);
  includeElement[ind] = 1;
  subset(includeElement, ind + 1, size, maxSize);
}

void findSubsets(int size, int maxSize)
{
  int * includeElement = malloc(sizeof(int) * size);
  subset(includeElement, 0, size, maxSize);
  free (includeElement);
}
